﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using Common;

public partial class website_admin_sales_ordermanagelist : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        switch (RequestBase.GetFormString("otype"))
        {
            case "loadingdata":
                LoadingData();
                break;
            case "deleteitems":
                Delete();
                break;
        }
    }
     
    #region 信息列表
    /// <summary>
    /// 信息列表
    /// </summary>
    void LoadingData()
    {
        try
        {
            StringBuilder SQL = new StringBuilder(); 
            if (!string.IsNullOrEmpty(RequestBase.GetFormString("username")))
            {
                SQL.Append(" AND (UserName LIKE '%" + RequestBase.GetFormString("username") + "%' OR FullName LIKE '%" + RequestBase.GetFormString("username") + "%' OR ShipToName LIKE '%" + RequestBase.GetFormString("username") + "%')");
            }

            if (!string.IsNullOrEmpty(RequestBase.GetFormString("ordernumber")))
            {
                SQL.Append(" AND OrderNumber = '" + RequestBase.GetFormString("ordernumber") + "'");
            }

            if (!string.IsNullOrEmpty(RequestBase.GetFormString("orderstatus")))
            {
                SQL.Append(" AND StatusCode = " + RequestBase.GetFormString("orderstatus"));
            }

            if (DateUtil.IsDateTime(RequestBase.GetFormString("date_from")) && DateUtil.IsDateTime(RequestBase.GetFormString("date_to")))
            {
                SQL.Append(" AND (DateDiff(DAY, OrderDate,'" + RequestBase.GetFormString("date_from") + "') <= 0 AND DateDiff(DAY, OrderDate,'" + RequestBase.GetFormString("date_to") + "') >= 0 ) ");
            }
            else
            {
                if (DateUtil.IsDateTime(RequestBase.GetFormString("date_from")))
                    SQL.Append(" AND DateDiff(DAY, OrderDate, '" + RequestBase.GetFormString("date_from") + "')=0 ");

                if (DateUtil.IsDateTime(RequestBase.GetFormString("date_to")))
                    SQL.Append(" AND DateDiff(DAY, OrderDate, '" + RequestBase.GetFormString("date_to") + "')=0 ");
            }

            Control uc_rep = LoadControl("~/website_admin/sales/_controls/ctlOrderList.ascx");
            Repeater rptOrderList = (Repeater)uc_rep.FindControl("rptOrderList");
            DBUtility.Pagination pagination = new DBUtility.Pagination();

            if (!string.IsNullOrEmpty(RequestBase.GetString("index")))
                pagination.Sequence = Convert.ToInt32(RequestBase.GetString("index"));
            else
                pagination.Sequence = 1;

            pagination.ItemUnit = "条记录";
            pagination.SortSentence = " ORDER BY OrderDate DESC";
            pagination.Primarykey = "OrderId";
            pagination.SearchField = "*";
            pagination.TableName = "vw_OrderBase";
            if (!string.IsNullOrEmpty(RequestBase.GetString("pagesize")))
                pagination.PageSize = Convert.ToInt32(RequestBase.GetString("pagesize"));
            else
                pagination.PageSize = 20;
            pagination.UrlStr = "";
            pagination.ImagesPath = "../_imgs/grid/";
            pagination.SearchSentence = SQL.ToString();

            rptOrderList.DataSource = pagination.GetDataTable();
            rptOrderList.DataBind();

            pagination.PageBarFlexigrid();

            uc_rep.GetType().GetProperty("PaginationBar").SetValue(uc_rep, pagination.PageBar, null);

            string total = BLL.CommonBLL.ExecuteScalar("vw_OrderBase", "ISNULL(SUM(TotalPrice), 0)", SQL.ToString()).ToString();
            uc_rep.GetType().GetProperty("TotalInfo").SetValue(uc_rep, total + "&nbsp;元", null);

            StringBuilder strb = new StringBuilder();
            StringWriter sw = new StringWriter(strb);
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            rptOrderList.RenderControl(htw);

            BLL.AjaxHelper.ResponseWrite(strb.ToString());
        }
        catch (System.Threading.ThreadAbortException err)
        {
            //nothing   to   do   
        }
        catch (Exception err)
        {
            BLL.AjaxHelper.ResponseWrite(err.Message);
        }
    }
    #endregion

    #region 删除
    /// <summary>
    /// 删除目录
    /// </summary>
    void Delete()
    {
        /// 逻辑删除商品记录
        string res = "";

        if (Validate("sales online", 6))
            foreach (string _id in RequestBase.GetFormString("itemsid").Split(','))
            {
                res = BLL.Insurance.InsurancePolicyBase.Delete(_id, true);
                if (res != "succeeded")
                    break;
            }
        else
            res = "当前用户无操作权限！";

        BLL.AjaxHelper.ResponseWrite(res);
    }
    #endregion
}